import { Metadata } from 'next';

import InvitePageClient from './InvitePageClient';

type Props = {
  params: Promise<{ slug: string }>;
};

export const metadata: Metadata = {
  robots: 'noindex',
};

export default async function InviteDefine(props: Props) {
  const params = await props.params;
  let data;
  try {
    data = await getData(params.slug);
  } catch (e) {
    console.log(e);
  }
  return <InvitePageClient profile={data} />;
}

async function getData(slug: string) {
  const lowercaseSlug = slug.toLowerCase();
  const handle = lowercaseSlug.replace(/^%40/, '');

  const requestUrl = `${process.env.API_BASE}/api/profiles/${handle}?playlists_sort_by=upvote_count&clips_sort_by=upvote_count&page=1`;
  const res = await fetch(requestUrl, {
    cache: 'no-store',
  });

  if (!res.ok) {
    throw new Error('Failed to fetch data');
  }

  return res.json();
}
